package com.xinchao.controller;

import com.xinchao.core.BaseController;
import com.xinchao.core.constant.PageConstant;
import com.xinchao.core.constant.ResponseConstant;
import com.xinchao.core.util.HttpUtils;
import com.xinchao.model.AsstTravelRecord;
import com.xinchao.model.User;
import com.xinchao.model.sys.JsonResponse;
import com.xinchao.service.AsstService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 播控助手线路controller
 * @Author: xp630
 * @Date: 2018/9/25 18:40
 */
@Controller
@RequestMapping("/route")
public class RouteController extends BaseController {
    @Autowired
    private AsstService asstService;
    private List<AsstTravelRecord> userRouteRecords;

    public static final String SUPER_ADMIN="superadmin";

    /**
     * 路线列表页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/list/index")
    public ModelAndView routeIndex(HttpServletRequest request) {
        return new ModelAndView("route_list");
    }

    /**
     * 获取当前用户的路线
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    public @ResponseBody
    Object routeList(HttpServletRequest request) {
        // 获取用户信息
        User loginUser = getSessionUser(request);
        String cityCode = loginUser.getCityCode();
        if (cityCode == null || cityCode.trim().equals("")) {
            return new ArrayList<>();
        }
        return asstService.getRouteListByCity(cityCode);
    }

    /**
     * 查询用户的路线
     * @param userId
     * @return
     */
    @RequestMapping(value = "/listbyuser/{userId}")
    public @ResponseBody
    Object userRouteList(@PathVariable("userId") Long userId) {
        if (userId == null || userId <= 0) {
            return new ArrayList<AsstTravelRecord>();
        }
        return asstService.getUserRouteRecords(userId);
    }

    @RequestMapping(value = "/listdata")
    public @ResponseBody
    Object routeListData(HttpServletRequest request,String routeName, String userName,Integer pageNum, Integer pageSize) {
        User current = getSessionUser(request);
        String cityCode = current.getCityCode();
        // 设置分页默认值
        pageNum = pageNum == null? PageConstant.DEFAULT_PAGE_NUM:pageNum;
        pageSize = pageSize == null?PageConstant.DEFAULT_PAGE_SIZE:pageSize;

        return asstService.listByPage(routeName,userName,cityCode,pageNum,pageSize);
    }

    /**
     * 绑定用户和路线关系
     * @param routeId 路线Id
     * @param userIds 用户Id
     * @param request
     * @return
     */
    @RequestMapping(value = "/rel/bind",method = RequestMethod.POST)
    public @ResponseBody Object bindUser( Long routeId,String userIds,HttpServletRequest request) {
        User current = getSessionUser(request);
        String cityCode = current.getCityCode();
        //解析路线信息
        List<Long> userList = new ArrayList<Long>();
        try {
            for(String userId:userIds.split(",")){
                userList.add(Long.parseLong(userId));
            }
        } catch (NumberFormatException e) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, "员工信息不正确");
        }
        asstService.bindRouteUsers(routeId,userList,cityCode, current, HttpUtils.getRemortIP(request));
        return "";
    }


}
